package org.example.bumenguanli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.bumenguanli.mapper.StudentMapper;
import org.example.bumenguanli.pojo.*;
import org.example.bumenguanli.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        //1. 设置PageHelper分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2. 执行查询
        List<Student> empList = studentMapper.list(empQueryParam);

       /* Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }*/
        //3. 封装分页结果
        Page<Student> p = (Page<Student>)empList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        if(student.getViolationCount() == null){
            student.setViolationCount((short) 0);
        }
        if(student.getViolationScore() == null){
            student.setViolationScore((short) 0);
        }
        studentMapper.save(student);
    }

    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getinfo(id);
    }

    @Override
    public void update(Student student) {
       studentMapper.update(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void updateViolation(Integer id, Short score) {
        studentMapper.updateViolation(id, score);
    }

    @Override
    public ClazzOption studentCountData() {
        List<Map<String, Object>> maps = studentMapper.studentCountData();
        List<Object> pos = maps.stream().map(map -> map.get("pos")).toList();
        List<Object> total = maps.stream().map(map -> map.get("total")).toList();
        return new ClazzOption(pos,total);
    }

    @Override
    public List<Map> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }
}
